package com.ljx.itbbs.server.user.controller;

import com.ljx.itbbs.common.domain.vo.resp.ApiResult;
import com.ljx.itbbs.common.utils.ServletUtil;
import com.ljx.itbbs.server.user.domain.vo.req.MenuSaveReq;
import com.ljx.itbbs.server.user.domain.vo.req.MenuUpdateReq;
import com.ljx.itbbs.server.user.domain.vo.resp.MenuTree;
import com.ljx.itbbs.server.user.domain.vo.resp.MenuResp;
import com.ljx.itbbs.server.user.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author LiJunXi
 * @since 2024-03-02
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("获取用户菜单")
    @GetMapping("/user/list")
    public ApiResult<List<MenuResp>> getUserMenu() {
        List<MenuResp> tbMenuList = menuService.getUserMenuList(ServletUtil.getUserId());

        return ApiResult.success(tbMenuList);
    }

    @ApiOperation("获取所有父菜单")
    @GetMapping("/parent/list")
    public ApiResult<List<MenuResp>> getParentMenu(){

        return ApiResult.success(menuService.getParentMenu());
    }

    @ApiOperation("获取所有菜单列表")
    @GetMapping("/all/list")
    public ApiResult<List<MenuTree>> getMenuList(){

        return ApiResult.success( menuService.getMenuList());
    }

    @ApiOperation("更新菜单")
    @PutMapping("/update")
    public ApiResult<Void> updateMenu(@RequestBody MenuUpdateReq req){
        menuService.updateMenu(req);

        return ApiResult.success();
    }

    @ApiOperation("保存菜单")
    @PostMapping("/save")
    public ApiResult<Void> saveMenu(@Valid @RequestBody MenuSaveReq req){
        menuService.saveMenu(req);

        return ApiResult.success();
    }

    @ApiOperation("管理员获取用户菜单")
    @GetMapping("/admin/user/list")
    public ApiResult<List<MenuResp>> getAdminUserMenu(@RequestParam("id") Long userId) {
        List<MenuResp> tbMenuList = menuService.getUserMenuList(userId);

        return ApiResult.success(tbMenuList);
    }


}

